﻿using MediatR;
using PublicClass;
using S.Organization.Domian.Model;
using S.Organization.Infrastructure;
using S.Organization.Read.API.Application.Command.FieldSettingCommand;

namespace S.Organization.Read.API.Application.CommandHandler.FieldSettingCommandHandler
{
    public class ShowFieldSettingCommandHandler:IRequestHandler<ShowFieldSettingCommand,PageInfo<FieldSetting>>
    {
        private readonly IBaseRepository<FieldSetting> _repository; 

        public ShowFieldSettingCommandHandler(IBaseRepository<FieldSetting> repository)
        {
            _repository = repository; 
        }

        public async Task<PageInfo<FieldSetting>> Handle(ShowFieldSettingCommand request, CancellationToken cancellationToken)
        {
            var list1 = await _repository.AllAsync();
            var query = list1.Where(x => x.CottageId == request.id);
            //数据总条数
            int Total = query.Count();
            //总页数
            int Count = (int)Math.Ceiling(Total * 1.0 / (request.Size));
    
            query = query.Skip((request.Index - 1) * request.Size).Take(request.Size);
              
            PageInfo<FieldSetting> list = new PageInfo<FieldSetting>();
            list.totalCount = Total;
            list.pageCount = Count;
            list.datas = query.ToList();
            return list;
        }
    }
}
